import json

import scrapy

from MedicareDrugs.items import MedicareDrugsItem


class pa(scrapy.Spider):
    name = 'MedicareDrugs'
    start_urls = [
        'https://code.nhsa.gov.cn/yp/getPublishGoodsDataInfo.html'
    ]
    custom_settings = {
        "USER_AGENT": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
        "USER_AGENT": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",
        "USER_AGENT": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0",
        "USER_AGENT": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
        "USER_AGENT": "Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50",
        "USER_AGENT": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)",
        "USER_AGENT": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)",
        "USER_AGENT": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)",
        "USER_AGENT": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
        "USER_AGENT": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E) QQBrowser/6.9.11079.201",
        "USER_AGENT": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201",
        "USER_AGENT": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)",
        "USER_AGENT": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0",
        "USER_AGENT": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)",

    }

    def start_requests(self):
        count = 2893
        for i in range(count):
            print("执行爬取第"+str(2893)+"页")
            data = {
                'companyNameSc': '',
                'registeredProductName': '',
                'approvalCode': '',
                'batchNumber': str(20210322),
                '_search': 'false',
                'nd': '1617760490144',
                'page': str(2893),
                'rows': str(50),
                'sidx': 't.goods_code',
                'sord': 'asc'
            }

            # 重写父类的方法start_urls
            for url in self.start_urls:
                # 直接发送get请求
                yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse)

    def parse(self, response):
        item = MedicareDrugsItem()
        # 这里拿到的是一个json  而不是源码 不需要使用response.xpath
        load_data = json.loads(response.text)
        dataJson = load_data.get("rows")
        for i in dataJson:
            item['goodscode'] = i.get('goodscode')
            item['registeredproductname'] = i.get('registeredproductname')
            item['goodsname'] = i.get('goodsname')
            item['registeredmedicinemodel'] = i.get('registeredmedicinemodel')
            item['registeredoutlook'] = i.get('registeredoutlook')
            item['materialname'] = i.get('materialname')
            item['factor'] = i.get('factor')
            item['minunit'] = i.get('minunit')
            item['unit'] = i.get('unit')
            item['companynamesc'] = i.get('companynamesc')
            item['approvalcode'] = i.get('approvalcode')
            item['goodsstandardcode'] = i.get('goodsstandardcode')
            item['productinsurancetype'] = i.get('productinsurancetype')
            item['productcode'] = i.get('productcode')
            item['productname'] = i.get('productname')
            item['productmedicinemodel'] = i.get('productmedicinemodel')
            item['productremark'] = i.get('productremark')
            yield item